package com.zyk.scaffold.tcp.disruptor;

import com.lmax.disruptor.EventHandler;
import com.zyk.scaffold.tcp.holder.ContextHolder;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;

@Slf4j
public class LocalEventHandler implements EventHandler<DisruptorMessage> {

    ExecutorService executorService;

    @Override
    public void onEvent(DisruptorMessage event, long sequence, boolean endOfBatch) throws Exception {
        ContextHolder.getPersistentLayerComponent().save(event.getMessageWrapper());
    }

    public static LocalEventHandler creatEventFactorys() {
        return new LocalEventHandler();
    }

    @PreDestroy
    public void shutdown() {
        if (executorService != null) {
            executorService.shutdown();
        }
    }
}
